package com.four.peoplemanage.excontracts.service;

import com.four.peoplemanage.common.bean.Page;
import com.four.peoplemanage.excontracts.dao.ExcontractsDao;
import com.four.peoplemanage.excontracts.entity.Excontracts;
import com.four.peoplemanage.overtime.entity.Overtime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class ExcontractsService {
    @Autowired
    ExcontractsDao excontractsDao;
    //新增合同
    private static int sequenceNumber = 0; // 全局变量，用于存储顺序号

    public boolean insert(Excontracts excontracts) {
        if (excontracts == null) {
            return false;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String dateString = sdf.format(new Date());

        // 同步代码块，确保线程安全
        synchronized (this) {
            sequenceNumber++; // 增加顺序号
            String sequenceNumberString = String.format("%03d", sequenceNumber); // 格式化为3位数字
            excontracts.setContract_number(dateString + "-" + sequenceNumberString); // 设置合同编号
        }

        return excontractsDao.addExcontracts(excontracts) > 0;
    }

    //查看合同
    public Page<Excontracts> getPageExcontracts (int pageNum, int pageSize, String name, String post, String type_contract){
        //计算起始位置
        int start=(pageNum-1)*pageSize;
        //获取分页的数据
        List<Excontracts> list=excontractsDao.getExcontracts(start,pageSize,name,post,type_contract);
        //获取总条目数
        int total=excontractsDao.countExcontracts(name,post,type_contract);
        //计算总页数
        int pages =(total % pageSize == 0 ? total / pageSize : total / pageSize + 1);
        //创建分页对象
        Page<Excontracts> page=new Page<>();
        page.setRecords(list);
        page.setTotal(total);
        page.setPages(pages);
        page.setCurrent(pageNum);
        page.setSize(pageSize);
        return page;
    }
}
